package starComponent;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import starComponent.elements.Ladder;
import starComponent.elements.StarBranchBiSlider;
import starComponent.elements.StarBranchBinaries;
import starComponent.elements.StarBranchSlider;
import starComponent.elements.StarComponent;
import tuio.components.ButtonTouch;
import tuio.components.TuioFrame;
import tuio.libTUIO.src.TUIO.TuioClient;

/**
 * @author Charles Perin
 * 21 avr. 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 */
public class StarVideoDependencies {

	/**
	 * @param args
	 */
	public static void main(String[] args) {


		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				TuioFrame frame = new TuioFrame("Video");
				TuioClient client = new TuioClient();
				client.addTuioListener(frame);
				client.connect();

				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


				frame.setPreferredSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize()));
				frame.setUndecorated(true);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

				final StarComponent star = new StarComponent(60,500,Color.BLUE);
				star.getControler().init();
				star.setBranchThickness(3);
				star.getControler().setLinkBranches(true);
				star.setLinkThickness(3);
				star.setCentralPolygonColor(Color.BLACK);
				star.setCentralPolygonThickness(1);
				star.getControler().setCollaborative(false);
				star.getControler().setPastTrace(false);
				star.getControler().setPastTraceTime(5000);
				star.getControler().setPastTraceFrequency(50);
				star.getControler().setDependenciesEnabled(true);
				star.getControler().setDependenciesFocusDisplayed(true);
				star.getControler().setDependenciesRangeDisplayed(true);
				star.getControler().setDependenciesLinkDisplayed(true);
				
				ButtonTouch exitButton = new ButtonTouch("Exit");
				exitButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {
						System.exit(0);
					}
				});


				ButtonTouch collaborativeButton = new ButtonTouch("collaborative");
				collaborativeButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {
						star.getControler().setCollaborative(!star.getControler().isCollaborative());
					}
				});

				ButtonTouch isLinkedButton = new ButtonTouch("link branches");
				isLinkedButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {
						star.getControler().setLinkBranches(!star.getControler().isLinkBranches());
					}
				});

				ButtonTouch isPastTraceButton = new ButtonTouch("past trace");
				isPastTraceButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {
						star.getControler().setPastTrace(!star.getControler().isPastTrace());
					}
				});


				frame.pack();
				frame.setVisible(true);

				frame.getContentPane().setLayout(new BorderLayout());

				JPanel rightPane = new JPanel();
				rightPane.setLayout(new BoxLayout(rightPane, BoxLayout.Y_AXIS));
				rightPane.add(exitButton);
				rightPane.add(collaborativeButton);
				rightPane.add(isLinkedButton);
				rightPane.add(isPastTraceButton);

				frame.getContentPane().add(rightPane, BorderLayout.EAST);
				frame.getContentPane().add(star, BorderLayout.CENTER);
				frame.pack();

				frame.setVisible(true);


				buildBranchList(star);

				try {
					Runtime.getRuntime().exec("cmd.exe /c start Touch2Tuio\\x64\\Release\\Touch2Tuio_x64.exe Video");
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
	}


	private static void buildBranchList(StarComponent star){
		star.initStar();
		
		//--------------------------------about lights---------------------------//
		
		StarBranchSlider lightPanAngle = new StarBranchSlider(
				star, 
				E_PossibleBranches.LIGHT_PAN_ANGLE,
				Color.RED,
				new Ladder(E_PossibleBranches.LIGHT_PAN_ANGLE,
						true,
						true,
						Color.BLACK,
						Color.BLACK,
						false), 
						Math.toRadians(160),
						true);
		lightPanAngle.setThumbColors(new Color[]{Color.BLACK, Color.GRAY}, new Color[]{Color.RED, new Color(255,200,200)});
		lightPanAngle.setValue(90);
		
		StarBranchSlider lightTiltAngle = new StarBranchSlider(
				star, 
				E_PossibleBranches.LIGHT_TILT_ANGLE,
				Color.RED,
				new Ladder(E_PossibleBranches.LIGHT_TILT_ANGLE,
						true,
						true,
						Color.BLACK,
						Color.BLACK,
						false), 
						Math.toRadians(180),
						true);
		lightTiltAngle.setThumbColors(new Color[]{Color.BLACK, Color.GRAY}, new Color[]{Color.RED, new Color(255,200,200)});
		lightTiltAngle.setValue(135);
		
		StarBranchSlider lightLuminosity = new StarBranchSlider(
				star, 
				E_PossibleBranches.LIGHT_LUMINOSITY,
				Color.RED,
				new Ladder(E_PossibleBranches.LIGHT_LUMINOSITY,
						true,
						true,
						Color.BLACK,
						Color.BLACK,
						false), 
						Math.toRadians(200),
						true);
		lightLuminosity.setThumbColors(new Color[]{Color.BLACK, Color.GRAY}, new Color[]{Color.RED, new Color(255,200,200)});
		lightLuminosity.setValue(90);
		
		//--------------------------------about camera---------------------------//

		StarBranchSlider dynamicity = new StarBranchSlider(
				star, 
				E_PossibleBranches.DYNAMICITY,
				Color.GREEN,
				new Ladder(E_PossibleBranches.DYNAMICITY,
						true,
						true,
						Color.BLACK,
						Color.GREEN,
						false), 
						Math.toRadians(0),
						true);
		dynamicity.setThumbColors(new Color[]{Color.BLACK, Color.GRAY}, new Color[]{Color.RED, new Color(255,200,200)});
		dynamicity.setValue(1);

		StarBranchSlider dutchAngle = new StarBranchSlider(
				star, 
				E_PossibleBranches.DUTCH_ANGLE, 
				Color.BLACK,
				new Ladder(E_PossibleBranches.DUTCH_ANGLE,
						true, 
						true, 
						Color.BLACK, 
						Color.BLACK,
						false),
						Math.toRadians(40),
						true);
		dutchAngle.setThumbColors(new Color[]{Color.BLACK, Color.GRAY}, new Color[]{Color.RED, new Color(255,200,200)});

		StarBranchSlider minShotDuration = new StarBranchSlider(
				star, 
				E_PossibleBranches.MIN_SHOT_DURATION, 
				Color.BLUE,
				new Ladder(E_PossibleBranches.MIN_SHOT_DURATION,
						true, 
						true, 
						Color.RED, 
						Color.RED,
						false),
						Math.toRadians(300),
						true);
		minShotDuration.setThumbColors(new Color[]{Color.BLACK, Color.GRAY}, new Color[]{Color.RED, new Color(255,200,200)});

		StarBranchSlider shotType = new StarBranchSlider(
				star, 
				E_PossibleBranches.SHOT_TYPE, 
				Color.BLACK,
				new Ladder(E_PossibleBranches.SHOT_TYPE,
						true, 
						true, 
						Color.BLACK, 
						Color.BLACK,
						false),
						Math.toRadians(120),
						true);
		shotType.setThumbColors(new Color[]{Color.BLACK, Color.GRAY}, new Color[]{Color.RED, new Color(255,200,200)});
		shotType.setValue(2);
		
		StarBranchBiSlider pacing = new StarBranchBiSlider(
				star, 
				E_PossibleBranches.PACING,
				Color.BLACK,
				new Ladder(E_PossibleBranches.PACING, 
						true, 
						true,
						Color.BLACK,
						Color.BLACK,
						false),
				Math.toRadians(230),
				true);
		pacing.setRangeColor(Color.BLUE);
		pacing.setMinRange(10.0);
		pacing.setRangeThickness(10);
		pacing.setMinValue(25);
		
		StarBranchSlider cameraAngle = new StarBranchSlider(
				star, 
				E_PossibleBranches.CAMERA_ANGLE, 
				Color.BLUE,
				new Ladder(E_PossibleBranches.CAMERA_ANGLE,
						true, 
						true, 
						Color.BLACK, 
						Color.BLACK,
						false),
						Math.toRadians(300),
						false);
		cameraAngle.setThumbColors(new Color[]{Color.BLACK, Color.GRAY}, new Color[]{Color.RED, new Color(255,200,200)});

		StarBranchSlider transitionType = new StarBranchSlider(
				star, 
				E_PossibleBranches.TRANSITION_TYPE, 
				Color.BLUE,
				new Ladder(E_PossibleBranches.TRANSITION_TYPE,
						true, 
						true, 
						Color.BLUE, 
						Color.BLUE,
						false),
						Math.toRadians(330),
						false);
		transitionType.setThumbColors(new Color[]{Color.BLACK, Color.GRAY}, new Color[]{Color.RED, new Color(255,200,200)});
		
		StarBranchBinaries cameraBinaries = new StarBranchBinaries(
				star,
				E_PossibleBranches.CAMERA_BINARIES, 
				Color.GRAY,
				new Ladder(E_PossibleBranches.CAMERA_BINARIES, true, true, Color.BLACK),
				Math.toRadians(330),
				true);

		StarBranchSlider intensity = new StarBranchSlider(
				star, 
				E_PossibleBranches.INTENSITY, 
				Color.BLUE,
				new Ladder(E_PossibleBranches.INTENSITY,
						true, 
						true, 
						Color.BLUE, 
						Color.BLUE,
						false),
						Math.toRadians(270),
						true);
		intensity.setValue(50);
		intensity.setThumbColors(new Color[]{Color.BLACK, Color.GRAY}, new Color[]{Color.RED, new Color(255,200,200)});
		
		

		star.getControler().addBranch(minShotDuration);
		star.getControler().addBranch(dutchAngle);
		star.getControler().addBranch(dynamicity);
		star.getControler().addBranch(shotType);
		star.getControler().addBranch(pacing);
		star.getControler().addBranch(cameraAngle);
		star.getControler().addBranch(transitionType);
		star.getControler().addBranch(lightLuminosity);
		star.getControler().addBranch(lightPanAngle);
		star.getControler().addBranch(lightTiltAngle);
		star.getControler().addBranch(cameraBinaries);
		star.getControler().addBranch(intensity);

		star.getControler().orderBranchesByAngle();
		star.getControler().createAllBranches();
		star.getControler().createDependencies();
		
		star.revalidate();
	}
}
